﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class Ad_AddAD : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("300400105");
        if (!this.IsPostBack)
        { 
            string  adid = Request.QueryString["adid"];
            if (adid != null)
            {
                ShowAd(adid);
            }
        
        }
    }
    protected void btnAllSave_Click(object sender, EventArgs e)
    {
        BLL.Ad adbll = new BLL.Ad();


        Model.Ad admodel = null;
        if (liadid.Text.Trim() != "")
        {
            admodel = adbll.GetModel(liadid.Text);

        }
        if (admodel == null)
        {
            admodel = new Model.Ad();
            admodel.Adid = Guid.NewGuid().ToString();
            admodel.AdName = txtADName.Text;
            admodel.AdPicAddress = fuAdPicAddress.FileName;
            admodel.AdPicUrl = txtAdPicUrll.Text;
        
            admodel.AdLink = txtAdLink.Text;

            admodel.AdAttribute4 = txtAdAttribute4.Text;
            admodel.AdAttribute6 = 1;
       



            admodel.AdAttribute3 = txtAdAttribute3.Text;
            admodel.AdAttribute1 = ddlpicchar.SelectedValue;

            if (rdbIsRunStateO.Checked == true)
            {
                admodel.IsRunState = 1;

            }
            else
            {
                admodel.IsRunState = 0;
            }

            if (TxtAdPicEndDateTim.Text != "")
            {
                admodel.AdPicEndDateTim = Convert.ToDateTime(TxtAdPicEndDateTim.Text);
            }
            if (TxtAdPicStartDateTime.Text != "")
            {
                admodel.AdPicStartDateTime = Convert.ToDateTime(TxtAdPicStartDateTime.Text);
            }
            admodel.AdPicAddress = UpdateFile("imgtem\\");
            adbll.Add(admodel);
        }
        else
        {
            admodel.AdName = txtADName.Text;
            admodel.AdAttribute3 = txtAdAttribute3.Text;
            admodel.AdPicUrl = txtAdPicUrll.Text;
            admodel.AdLink = txtAdLink.Text;
            admodel.AdAttribute4 = txtAdAttribute4.Text;
            admodel.AdAttribute1 = ddlpicchar.SelectedValue;

            if (TxtAdPicEndDateTim.Text != "")
            {
                admodel.AdPicEndDateTim = Convert.ToDateTime(TxtAdPicEndDateTim.Text);
            }
            if (TxtAdPicStartDateTime.Text != "")
            {
                admodel.AdPicStartDateTime = Convert.ToDateTime(TxtAdPicStartDateTime.Text);
            }
            if (rdbIsRunStateO.Checked == true)
            {
                admodel.IsRunState = 1;

            }
            else
            {
                admodel.IsRunState = 0;
            }

            if (fuAdPicAddress.FileName != "")
            {
                if (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath("~/") + admodel.AdPicAddress))
                {
                    File.Delete(System.Web.HttpContext.Current.Server.MapPath("~/") + admodel.AdPicAddress);
                    admodel.AdPicAddress = UpdateFile("imgtem\\");
                }
            }
            adbll.Update(admodel);
        

           
        }
        Response.Redirect("Admanager.aspx");
    }

    public string  UpdateFile(string path)
    {
        string serverPath = System.Web.HttpContext.Current.Server.MapPath(".") + "//";//服务器路径
        string savePath = "";//用于返回的路径   保存到数据库的
        string upPath = "";//上传的路径;
   
        string name = DateTime.Now.ToString("yyyyMMddHHmmss");//重新命名
        bool bl = false;//用于判断文件后缀
        string[] str ={ "jpg", "png", "gif", "bmp" };
        if (fuAdPicAddress.FileName != "")
        {
            string str1 = fuAdPicAddress.FileName;//用户给的路径
            string houzhui = str1.Substring(str1.LastIndexOf(".") + 1).ToLowerInvariant();//后缀名
            if (houzhui.Length < 3 && houzhui.Length >= 0)
            {
                houzhui.PadRight(3);
            }
            //判断图片格式是否正确
            for (int i = 0; i < str.Length; i++)
            {
                if (houzhui == str[i].ToString())
                {
                    bl = true;
                    break;
                }
            }
            if (bl)
            {
                savePath = path + name + "." + houzhui;//用于返回的路径   保存到数据库的
                upPath = System.Web.HttpContext.Current.Server.MapPath("~/") + path + name + "." + houzhui;
                fuAdPicAddress.SaveAs(upPath);//上传截图
            }

        }
        return savePath;
    }

    public void ShowAd(string  adid)
    {
        BLL.Ad adbll = new BLL.Ad();
        Model.Ad admodel = adbll.GetModel(adid);
        liadid.Text = admodel.Adid;
        txtAdAttribute3.Text =  admodel.AdAttribute3 ;
        txtADName.Text =  admodel.AdName;
        ddlpicchar.SelectedValue  =  admodel.AdAttribute1.Trim();
        txtAdAttribute4.Text  =  admodel.AdAttribute4;
        txtAdLink.Text = admodel.AdLink;
        //fuAdPicAddress.FileName;admodel.AdPicAddress
        txtAdPicUrll.Text  =  admodel.AdPicUrl;
        TxtAdPicEndDateTim.Text  = admodel.AdPicEndDateTim.ToString();
        TxtAdPicStartDateTime.Text  = admodel.AdPicStartDateTime.ToString();
        if (admodel.IsRunState == 1)
        {
            rdbIsRunStateC.Checked = false;
            rdbIsRunStateO.Checked = true;
        }
        else
        {
            rdbIsRunStateC.Checked = true;
            rdbIsRunStateO.Checked =false ;
        }
       
    }

}
